package com.csr.csrcheck.controller;

import com.csr.csrcheck.controller.ex.CompanyException;
import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.Lawsuit;
import com.csr.csrcheck.service.CompanyService;
import com.csr.csrcheck.service.LawsuitService;
import com.csr.csrcheck.service.ex.LawsuitException;
import com.csr.csrcheck.util.JsonResult;
import com.csr.csrcheck.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping({"lawsuit"})
@RestController
public class LawsuitConntroller extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.LawsuitConntroller.class);

    @Resource
    private LawsuitService lawsuitService;

    @Resource
    private CompanyService companyService;

    @GetMapping({"lawsuitlist"})
    public JsonResult<List<Lawsuit>> Companylist() {
        List<Lawsuit> list = this.lawsuitService.getLawsuitlist();
        if (list == null)
            throw new LawsuitException("");
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping({"lawsuit"})
    public JsonResult<Object> lawsuit(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, String company_name, String casetype, String doctype, String defendants, String submittime) {
        PageResult pageResult = this.lawsuitService.getlistpage(pageNum, pageSize, company_name, casetype, doctype, defendants, submittime);
        if (pageResult == null)
            throw new CompanyException("");
                    log.info("lawsuit-------------------------------------->pageNum:" + pageNum);
        log.info("lawsuit-------------------------------------->pageSize:" + pageSize);
        log.info("lawsuit-------------------------------------->company_name:" + company_name);
        log.info("lawsuit-------------------------------------->casetype:" + casetype);
        log.info("lawsuit-------------------------------------->doctype:" + doctype);
        log.info("lawsuit-------------------------------------->defendants:" + defendants);
        log.info("lawsuit-------------------------------------->submittime:" + submittime);
        return new JsonResult(Integer.valueOf(0), "json", pageResult);
    }

    @RequestMapping(path = {"addlawsuit"}, method = {RequestMethod.POST})
    public JsonResult<Void> addlawsuit(Lawsuit lawsuit) {
        this.lawsuitService.addlawsuit(lawsuit);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"updatelawsuit"}, method = {RequestMethod.POST})
    public JsonResult<Void> updatelawsuit(Lawsuit lawsuit) {
        this.lawsuitService.updatelawsuit(lawsuit);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"deletelawsuit/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deletelawsuit(@PathVariable("id") int id) {
        this.lawsuitService.deletelawsuit(id);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findlawsuit/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Lawsuit> findlawsuit(@PathVariable("id") int id) {
        Lawsuit lawsuit = this.lawsuitService.findlawsuit(id);
        return new JsonResult(Integer.valueOf(0), "json", lawsuit);
    }

    @RequestMapping(path = {"showcompany"}, method = {RequestMethod.POST})
    public JsonResult<List<Company>> showcompany() {
        List<Company> list = this.companyService.getCommpanylist();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }
}
